/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.data.provider.shp;

import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.ResultSet;

import chunmap.data.feature.FeatureSchama;
import chunmap.data.feature.LayerMetadata;
import chunmap.data.provider.FeatureDataReader;
import chunmap.model.elem.Envelope;
import chunmap.model.geom.Geometry;

/**
 * @author chunquedong
 * 
 */
public class ShpDataSource implements FeatureDataReader {
	private final ShapefileReader shpReader;
	private DbfReader dbfReader;

	public ShpDataSource(String path) {
		this(path,null);
	}
	
	public ShpDataSource(String path,Charset charset) {
		this.shpReader = new ShapefileReader(path);
		dbfReader=new DbfReader(path);
		dbfReader.setCharset(charset);
	}

	@Override
	public void close() {
		shpReader.close();
		
	}

	@Override
	public Object[] data(long index) {
		return dbfReader.getValues(index);
	}

	@Override
	public Envelope getEnvelop() {
		return shpReader.getEnvelop();
	}

	@Override
	public FeatureSchama getFeatureSchama() {
		return dbfReader.getFeatureSchama();
	}

	@Override
	public Geometry getGeometry(long id) {
		try {
			return shpReader.getGeometry(id);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public LayerMetadata getMetadata() {
		LayerMetadata meta = new LayerMetadata(shpReader.getName(),shpReader.getGeometryType());
        return meta;
	}

	@Override
	public Envelope getShapeEnvelop(long id) {
		try {
			return shpReader.getShapeEnvelop(id);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new Envelope();
	}

	@Override
	public ResultSet query(String where) {
		throw new UnsupportedOperationException();
	}

	@Override
	public long size() {
		return shpReader.getShapeCount();
	}

}
